var queues = require('mysql-queues');
const DEBUG = true;
var select = function(connection, callback) {
	var sql = 'select * from project';
	connection.query(sql, function(err, rows) {
		if (err){
			console.log(err);
			console.log(err.stack);
		}
		callback(rows);
		connection.release();
	});
},
	add = function(connection, data, callback) {
		queues(connection, DEBUG);
		var sql = 'insert into project(prjName,orgId,startDate,endDate,employeeId,partyb) values(?,?,?,?,?,?)';
		var trans = connection.startTransaction(); 
		trans.query(sql, [data.prjName, data.orgId, data.startDate, data.endDate, data.employeeId, data.partyb], function(err, rows) {
			if (err) {
				console.log(err);
				console.log(err.stack);
				trans.rollback();
			} else {
				trans.commit(function(err, info) {
					console.log(info);
				});
			}
			callback(rows);
		});
		trans.execute();
		console.log('execute addUserRole');
		connection.release();
	},
	delet = function(connection, id, callback) {
		queues(connection, DEBUG);
		var sql = 'delete from project where id=?';
		var trans = connection.startTransaction();
		trans.query(sql, id, function(err, rows) {
			if (err) {
				console.log(err);
				console.log(err.stack);
				trans.rollback();
			} else {
				trans.commit(function(err, info) {
					console.log(info);
				});
			}
			console.log('rows', rows);
			callback(rows);
		});
		trans.execute();
		console.log('execute');
		connection.release();
	},
	update = function(connection, data, callback) {
		queues(connection, DEBUG);
		var sql = 'update project set prjName=?,orgId=?,startDate=?,endDate=?,employeeId=?,partyb=? where id=?';
		var trans = connection.startTransaction();
		trans.query(sql, [data.prjName, data.orgId, data.startDate, data.endDate, data.employeeId, data.partyb, data.id], function(err, rows) {
			if (err) {
				console.log(err);
				console.log(err.stack);
				trans.rollback();
			} else {
				trans.commit(function(err, info) {
					console.log('info:', info);
				});
			}
			console.log('rows', rows);
			callback(rows);
		});
		trans.execute();
		console.log('execute update');
		connection.release();
	},
	findById = function(connection, id, callback) {
		var sql = 'select * from project where id=?';
		connection.query(sql, id, function(err, rows) {
			callback(rows);
			connection.release();
		});
	};

module.exports = {
	selectProject: select,
	addProject: add,
	deleteProject: delet,
	updateProject: update,
	findById: findById
};